package net.jjjshop.front.controller.lbt;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.lbt.LbUserBankEntity;
import net.jjjshop.common.entity.lbt.UserExtractEntity;
import net.jjjshop.common.entity.lbt.UserExtractSearchRequest;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.front.controller.BaseController;
import net.jjjshop.front.param.user.UserExtractRequest;
import net.jjjshop.front.service.lbt.LbUserBankService;
import net.jjjshop.front.service.lbt.LbUserExtractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Api(value = "order", tags = {"购物车管理"})
@RestController
@RequestMapping("/front/lbt/userBank")
public class UserBankController  extends BaseController {

    @Autowired
    private LbUserBankService userBankService;


    @Autowired
    private LbUserExtractService userExtractService;
    /**
     * 提现申请
     */
    @ApiOperation(value = "提现申请")
    @RequestMapping(value = "/extractApply", method = RequestMethod.POST)
    public ApiResult<String> extractApply(@RequestBody @Validated UserExtractRequest request) {
        Boolean apply = userExtractService.extractApply(request);
        if (apply){
            return ApiResult.ok(null,"申请成功");
        }else{
            return ApiResult.fail("申请失败");
        }
    }

    @ApiOperation(value = "用户提现分页")
    @RequestMapping(value = "/userList", method = RequestMethod.POST)
    public ApiResult<IPage<UserExtractEntity>>  getList( Integer page, Integer limit){
        return ApiResult.ok(userExtractService.getUserList( page,limit));
    }
    /**
     * 新增银行卡
     */
    @ApiOperation(value = "用户新增银行卡")
    @PostMapping("/addBank")
    public ApiResult<String> addBank(@RequestBody @Validated LbUserBankEntity bank){
        User user = this.getUser(true);
        if (user.getUserId().equals(bank.getUid())){
        if (null != bank.getId() && bank.getId() >0){
            if( userBankService.updateById(bank)){
                return ApiResult.ok("编辑成功");
            }
            return ApiResult.fail("编辑失败");
        }
        }
        bank.setCreateTime(new Date());
        bank.setUid(user.getUserId());
        bank.setAppId(user.getAppId());
        if( userBankService.save(bank)){
            return ApiResult.ok("绑卡成功");
        }

        return ApiResult.fail("绑卡失败");
    }

    /**
     * 查询用户绑卡列表
     */
    @ApiOperation(value = "查询用户绑卡列表")
    @GetMapping("/bankList")
    public ApiResult<List<LbUserBankEntity>> bankList(){
        return ApiResult.ok(userBankService.getList(null));
    }

    /**
     * 用户删除银行卡
     */
    @ApiOperation(value = "用户删除银行卡")
    @PostMapping("/deleteBank")
    public ApiResult<String> deleteBank(@RequestParam("id") Integer id){
        User user = this.getUser(true);
        QueryWrapper<LbUserBankEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("uid",user.getUserId());
        queryWrapper.eq("app_id",user.getAppId());
        if (userBankService.remove(queryWrapper)){
            return ApiResult.ok("删除成功");
        }
        return ApiResult.fail("删除失败");
    }



}
